<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="项目名称" prop="sportlistName">
        <el-input
          v-model="queryParams.sportlistName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sportlists:sportlist:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sportlists:sportlist:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sportlists:sportlist:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sportlists:sportlist:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sportlistList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="项目id" align="center" prop="sportlistId" /> -->
      <el-table-column
        label="项目名称"
        align="center"
        prop="sportlistName"
        width="108px"
      />
      <el-table-column
        label="项目主题内容"
        align="center"
        prop="sportlistText"
        width="250px"
      />
      <el-table-column
        label="项目状态"
        align="center"
        prop="sportStatus"
        width="108px"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sportStatus == 0">
            <el-button type="primary" :loading="true" round>可申报</el-button>
          </span>
          <span v-if="scope.row.sportStatus == 1">
            <el-button type="danger" round>不可申报</el-button>
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="项目可申报人数"
        align="center"
        prop="sportlistSum"
        width="120px"
      />
      <el-table-column
        label="项目裁判员"
        align="center"
        prop="refereesName"
        width="110px"
      />

      <el-table-column
        label="项目裁判员等级"
        align="center"
        prop="refereesLevel"
        width="150px"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.refereesLevel == 0">
            <el-button type="primary" round>国家一级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 1">
            <el-button type="primary" round>国家二级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 2">
            <el-button type="primary" round>国家三级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 3">
            <el-button type="primary" round>国家特级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 4">
            <el-button type="primary" round>其他</el-button>
          </span>
        </template>
      </el-table-column>

      <el-table-column label="裁判员序号" align="center" prop="sportferessid"/>
      <el-table-column label="项目备注" align="center" prop="sportlistRemark" />
      <el-table-column
        label="项目开始时间"
        align="center"
        prop="sportStart"
        width="160px"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sportStart, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="项目结束时间"
        align="center"
        prop="sportEnd"
        width="120px"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sportEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sportStatus == 0">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="baoming(scope.row)"
              >可申报</el-button
            >
          </span>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <!-- <span v-if="scope.row.sportStatus == 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              disabled="disabled"
              >已申报</el-button
            >
          </span> -->
          &nbsp;&nbsp;&nbsp;&nbsp;
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sportlists:sportlist:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sportlists:sportlist:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运动会具体项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="99px">
        <!-- <el-form-item label="运动会id" prop="sprotInformId">
          <el-input v-model="form.sprotInformId" placeholder="请输入运动会id" />
        </el-form-item> -->
        <el-form-item label="项目名称" width="130px" prop="sportlistName">
          <el-input v-model="form.sportlistName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目主题内容" width="130px" prop="sportlistText">
          <el-input
            v-model="form.sportlistText"
            placeholder="请输入项目主题内容"
          />
        </el-form-item>
        <el-form-item label="运动会状态" prop="sportStatus">
          <el-select v-model="form.sportStatus" placeholder="请选择输出情况">
            <el-option label="不可申报" value="1" />
            <el-option label="可申报" value="0" />
          </el-select>
        </el-form-item>

        <el-form-item label="项目可申报人数" prop="sportlistSum">
          <el-input
            v-model="form.sportlistSum"
            placeholder="请输入项目可申报人数"
          />
        </el-form-item>

        <!-- 输入运动会关联裁判员 -->
        <el-form-item label="选择裁判员" prop="refereesName">
          <el-select
            @change="matProferees"
            v-model="form.refereesName"
            placeholder="请选择裁判员名称"
          >
            <el-option
              v-for="item in sportlistrefereeslevels"
              :key="item.value"
              :label="item.label"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="裁判员等级" prop="refereesLevel">
          <el-select :disabled="true" v-model="form.refereesLevel">
            <el-option label="国家二级" value="1" />
            <el-option label="国家一级" value="0" />
            <el-option label="国家三级" value="2" />
            <el-option label="国家特级" value="3" />
            <el-option label="其他" value="4"/>
          </el-select>
        </el-form-item>

        <el-form-item label="项目备注" prop="sportlistRemark">
          <el-input
            v-model="form.sportlistRemark"
            placeholder="请输入项目备注"
          />
        </el-form-item>

        <el-form-item label="裁判员id" prop="sportferessid">
          <el-input v-model="form.sportferessid" :disabled="true" placeholder="裁判员id"/>
        </el-form-item>
        <el-form-item label="项目开始时间" prop="sportStart">
          <el-date-picker
            clearable
            v-model="form.sportStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择项目开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目结束时间" prop="sportEnd">
          <el-date-picker
            clearable
            v-model="form.sportEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择项目结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSportlist,
  getSportlist,
  delSportlist,
  addSportlist,
  updateSportlist,
  getSportFeress,
} from "@/api/sportlists/sportlist";
//申报api
import { addDeclareInformation } from "@/api/sportDeclareInformation/declareInformation";
export default {
  name: "Sportlist",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运动会具体项目表格数据
      sportlistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sportlistName: null,
        sportlistText: null,
        sportStatus: null,
        sportlistSum: null,
        sportlistRemark: null,
        sportferessid: null, //裁判员id
        sportStart: null,
        sportEnd: null,
        sprotInformId: null,
        remark: null,
        sportlistrefereeslevel: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dataList: [],
      setSportDeInformName: null,
      _rowfu: {},
      sportlistrefereeslevels: [], //裁判组信息
    };
  },
  created() {
    //把运动会的id 赋值给
    this.queryParams.remark = this.$route.params.rowlist.sportId;
    this.getList();
    this.getSportFeress(); //获取裁判组信息
  },
  methods: {
    getSportFeress() {
      this.loading = true;
      getSportFeress().then((response) => {
        this.sportlistrefereeslevels = response.data;
      });
    },

    //获取裁判组信息水平进行赋值
    matProferees(item) {
      console.log(item);
      this.form.sportferessid = item.value;
      this.form.refereesLevel = item.level;
      this.form.refereesName = item.label;
    },

    //学生进行报名的方法
    baoming(row) {
      //使用中间值进行赋值操作
      row.remark = this.$route.params.rowlist.sportName;
      // console.log(row);
      {
        const sportlistName = row.sportlistName;
        this.$modal
          .confirm(
            '是否确认申报运动会具体项目名称为"' + sportlistName + '"的数据项'
          )
          .then(function () {
            return addDeclareInformation(row);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("申报成功"); //去更新项目状态 禁止重复报名
            this.getList();
          })
          .catch(() => {
            // this.$modal.msgError("你已经申报此项目运动了");
          });
      }
    },

    /** 查询运动会具体项目列表 */
    getList() {
      this.loading = true;
      listSportlist(this.queryParams).then((response) => {
        console.log(response.rows)
        this.sportlistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sportlistId: null,
        sprotInformId: null,
        sportlistName: null,
        sportlistText: null,
        sportStatus: null,
        sportlistSum: null,
        sportlistRemark: null,
        sportStart: null,
        sportEnd: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.sportlistId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.sprotInformId = this.$route.params.rowlist.sportId;
      this.title = "添加运动会具体项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row.sportlistId);
      this.reset();
      const sportlistId = row.sportlistId || this.ids;
      getSportlist(sportlistId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运动会具体项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form.sportlistId);
          if (this.form.sportlistId != null) {
            updateSportlist(this.form).then((response) => {
              console.log(response);
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("-------------------");
            console.log(this.form);
            addSportlist(this.form).then((response) => {
              console.log(response);
              this.$modal.msgSuccess("新增成功");
              console.log(this.form);
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sportlistIds = row.sportlistId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除运动会具体项目编号为"' + sportlistIds + '"的数据项？'
        )
        .then(function () {
          return delSportlist(sportlistIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sportlists/sportlist/export",
        {
          ...this.queryParams,
        },
        `sportlist_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
